package com.yllt4cloud.common.ddbase.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 *
 * @author Administrator
 */
public class Base64Utils {
    private  static final Logger LOGGER = LoggerFactory.getLogger(Base64Utils.class);
    private Base64Utils() {
    }

    public static byte[] decode(String content) {
        return  DatatypeConverter.parseBase64Binary(content);
    }

    public static String decodeToString(String content) {
        return new String(decode(content));
    }

    public static String decodeToString(String content, String charSet) {
        return  decodeToString(content, Charset.forName(charSet));
    }

    public static String decodeToString(String content, Charset charset) {
        return new String(decode(content), charset);
    }

    public static String encode(byte[] bytes) {
        String base64String = DatatypeConverter.printBase64Binary(bytes);
        return base64String.replace("\n", "").replace("\r", "");
    }
   
    
    public static void main(String[] args) throws UnsupportedEncodingException {
		String enc = encode("{\"token\":\"7613e58bd9bc40e4\",\"user\":\"123456\"}".getBytes("UTF-8"));
		System.out.println(enc);
		System.out.println(decodeToString(enc));
	}
}
